/**
 * 
 */
package controller;

import adapter.SMSLogAdapter;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ListView;

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.MenuItem;
import com.example.remotecontrol.R;

import common.DimensionHelper;
import components.SMSLogManager;

/**
 * @author User
 * 
 */
public class SMSLogFragment extends SherlockFragment implements OnItemSelectedListener{

	private ListView mListView;
	private SMSLogAdapter adapter;
	/**
	 * 
	 */
	public SMSLogFragment() {
		// TODO Auto-generated constructor stub\
		//setHasOptionsMenu(true);
	}

	@SuppressLint("NewApi")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		
		
		View view = inflater.inflate(R.layout.sms_log_fragment, container,
				false);

		mListView = (ListView) view.findViewById(R.id.list_view);
		adapter = new SMSLogAdapter(getActivity(), SMSLogManager.sharedSMSLogManager(getActivity()).getSMSLogList());
		
		mListView.setAdapter(adapter);
//		mListView.setOnItemSelectedListener(new OnItemSelectedListener() {
//			@Override
//			public void onItemSelected(AdapterView<?> arg0, View arg1,
//					int arg2, long arg3) {
//				// TODO Auto-generated method stub
//
//				if (arg3 == 0) {
//						//TODO: Push to Fragment Set text
//					Toast.makeText(getActivity(), "Selected", Toast.LENGTH_LONG).show();
//				}
//			}
//			
//			@Override
//			public void onNothingSelected(AdapterView<?> arg0) {
//				// TODO Auto-generated method stub
//				
//			}
//		});
//		
		mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				if (arg3 == 0) {
					//TODO: Push to Fragment Set text
//					Fragment phone = FragmentMediator.CreateFragment(FragmentMediator.FRAGMENT_PHONE);
//					FragmentMediator.transitView(SMSLogFragment.this, phone, null);
			} else if (arg3 == 1) {
//				Fragment passcode = FragmentMediator.CreateFragment(FragmentMediator.FRAGMENT_PASSCODE);
//				FragmentMediator.transitView(SMSLogFragment.this, passcode, null);
			}
			}
		});
		
		
		
		return view;
	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		
		
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		setHasOptionsMenu(true);
		getView().findViewById(R.id.root_layout).setPadding(
				0, DimensionHelper.getPxFromDp(getActivity(), 100), 0, 0);
		
		adapter.notifyDataSetChanged();
	}
	
	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) {
		// TODO Auto-generated method stub
		super.setUserVisibleHint(isVisibleToUser);
		
		if (isVisibleToUser) {
			getActivity().invalidateOptionsMenu();
		}
	}
	@Override
	public void onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu,
			com.actionbarsherlock.view.MenuInflater inflater) {
		// TODO Auto-generated method stub
		super.onCreateOptionsMenu(menu, inflater);
		inflater.inflate(R.menu.sms_log_manager, menu);
	}
	
	@Override
	public void onPrepareOptionsMenu(com.actionbarsherlock.view.Menu menu) {
		// TODO Auto-generated method stub
		super.onPrepareOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.menu_settings:
			SMSLogManager.sharedSMSLogManager(getActivity()).clear(getActivity());
			adapter.notifyDataSetChanged();
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
}